package com.hnld.transform.util;

import cn.afterturn.easypoi.csv.CsvImportUtil;
import cn.afterturn.easypoi.csv.entity.CsvImportParams;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import com.hnld.transform.annotation.Change;
import com.hnld.transform.cosntant.TransformConstant;
import com.hnld.transform.entity.IRIRoute;
import com.hnld.transform.entity.LBIRoute;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class CSVFileUtils {

    public static <T> List<T> readCSVData(String filePath, Class<T> clazz, boolean skipHead) {
        // 创建 CSV 导入参数对象
        CsvImportParams csvParams = new CsvImportParams(CsvImportParams.UTF8);
        if (skipHead) {
            csvParams.setTitleRows(1);
        }
        List<T> objects = CsvImportUtil.importCsv(FileUtil.getInputStream(filePath), clazz, csvParams);
        if (CollectionUtil.isEmpty(objects)) {
            return objects;
        }
        Object o = objects.get(0);
        Field[] declaredFields = clazz.getDeclaredFields();
        int count = 0;
        try {
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                Object value = declaredField.get(o);
                if (value == null) {
                    count++;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (count > 2) {
            //说明编码格式不对
            csvParams.setEncoding(CsvImportParams.GB2312);
            return CsvImportUtil.importCsv(FileUtil.getInputStream(filePath), clazz, csvParams);
        }
        return objects;
    }


    public static void main(String[] args) {
  /*      List<User> list = readCSVData("C:\\Users\\HNLD-005\\Desktop\\C00A431102A.csv", User.class);
        for (User data : list) {
            System.out.println(JSONObject.toJSON(data));
        }*/
        List<IRIRoute> lbiRoutes = readCSVData("E:\\报部数据转格式\\零陵区//平整度数据/IRI//C003431102-IRI-K0+636-20230729082154.csv", IRIRoute.class, false);
        List<LBIRoute> lbiRoutes1 = readCSVData("C:\\Users\\HNLD-005\\Desktop\\C438431102A-LBI-0.000-20230822113002.csv", LBIRoute.class, false);
        List<String> titleList = getTitleList(IRIRoute.class, TransformConstant.excludeTitle, "识别宽度(m)", 1);
        System.out.println(titleList);
    }

    /**
     * 获取clz中带有@Excel注解的字段
     *
     * @param clz
     * @param excludeTitle
     * @return
     */
    public static List<String> getTitleList(Class<?> clz, List<String> excludeTitle, String extraTitle, int index) {
        // 获取所有字段
        Field[] fields = clz.getDeclaredFields();
        List<String> titleList = new ArrayList<>();
        for (Field field : fields) {
            Excel excelAnnotation = field.getAnnotation(Excel.class);
            if (excelAnnotation != null) {
                String originalName = excelAnnotation.name(); // 获取原始的Excel注解中的name值
                if (excludeTitle.contains(originalName)) {
                    continue;
                }
                //将originalName中的()替换为()
                String newName = originalName.replaceAll("\\（", "(").replaceAll("\\）", ")");
                // 检查是否同时存在@Change注解
                if (field.isAnnotationPresent(Change.class)) {
                    Change changeAnnotation = field.getAnnotation(Change.class);
                    // 如果存在@Change注解，则使用其value值替换Excel注解中的name值
                    newName = changeAnnotation.value();
                }
                titleList.add(newName);
            }
        }
        if (StringUtils.isNotBlank(extraTitle)) {
            titleList.add(index, extraTitle);
        }
        return titleList;
    }

/*    public static void writeData(List<?> csvDatas, String targetDir, String newFileName) {
        Class<?> aClass = csvDatas.get(0).getClass();
        for (Object csvData : csvDatas) {

        }
    }*/
}
